import {
  getCheckByMail,
  getCheckByMobile,
  getGetCode,
  postSetPasswordByMail,
  postSetPasswordByMobile,
  getValidMobileCode,
  getValidMailCode,
} from './services';

export default {
  namespace: 'resetPassword',
  state: {
    information: {
      // 找回密码类型，用邮箱或者手机 mobile/mail
      type: '',

      // 账号
      account: '',

      // 验证码
      code: '',
    },
  },
  effects: {
    *checkBymail({ payload }, { call }) {
      const response = yield call(getCheckByMail, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *checkBymobile({ payload }, { call }) {
      const response = yield call(getCheckByMobile, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *getCode({ payload }, { call }) {
      const response = yield call(getGetCode, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *setPasswordByMail({ payload }, { call }) {
      const response = yield call(postSetPasswordByMail, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *setPasswordByMobile({ payload }, { call }) {
      const response = yield call(postSetPasswordByMobile, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *setValidMobileCode({ payload }, { call, put }) {
      const response = yield call(getValidMobileCode, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *setValidMailCode({ payload }, { call, put }) {
      const response = yield call(getValidMailCode, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
  },
  reducers: {
    saveInformation(state, { payload }) {
      state.information = payload;
      return state;
    },
  },
};
